package com.aaa.doctor.service.impl;

import com.aaa.doctor.config.IDCardUtil;
import com.aaa.doctor.mapper.CareHistoryMapper;
import com.aaa.doctor.mapper.PatientFileMapper;
import com.aaa.doctor.mapper.PatientMapper;
import com.aaa.doctor.service.PatientService;
import com.aaa.doctor.vo.PatientVo;
import com.aaa.entity.CareHistory;
import com.aaa.entity.Patient;
import com.aaa.entity.PatientFile;
import com.aaa.vo.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @author MuChengGuo
 * @date 2023/3/24 10:48
 * Description: TODO
 **/
@Service
public class PatientServiceImpl implements PatientService {
    @Autowired(required = false)
    private PatientMapper patientMapper;
    @Autowired(required = false)
    private PatientFileMapper patientFileMapper;
    @Autowired(required = false)
    private CareHistoryMapper careHistoryMapper;

    /**
     * 条件&分页查询全体员工的信息
     *
     * @param current
     * @param size
     * @param patientVo
     * @return
     */
    @Override
    public Result<IPage<Patient>> patientPageList(Integer current, Integer size, PatientVo patientVo) {
        IPage page = new Page(current, size);
        QueryWrapper<Patient> wrapper = new QueryWrapper<>();
        if (StringUtils.hasText(patientVo.getName())) {
            wrapper.like("name", patientVo.getName());
        }
        if (StringUtils.hasText(patientVo.getPhone())) {
            wrapper.like("phone", patientVo.getPhone());
        }
        if (StringUtils.hasText(patientVo.getIdCard())) {
            wrapper.like("id_card", patientVo.getIdCard());
        }
        IPage iPage = patientMapper.selectPage(page, wrapper);
        for (Patient record : (List<Patient>) iPage.getRecords()) {
            Integer age = IDCardUtil.getAge(record.getIdCard());
            record.setAge(age);
        }
        return new Result<IPage<Patient>>(2000, "条件&分页查询全体员工的信息成功", iPage);
    }

    /**
     * 通过 patientId 查看此患者信息&患者档案
     *
     * @param patientId
     * @return
     */
    @Override
    public Result<Patient> getPatientById(String patientId) {
        QueryWrapper<Patient> wrapper = new QueryWrapper<>();
        wrapper.eq("patient_id", patientId);
        Patient patient = patientMapper.selectOne(wrapper);
        return new Result<>(2000, "通过 patientId 查看此患者档案成功", patient);
    }

    /**
     * 通过 身份证号(idCard) 查看此患者信息
     *
     * @param idCard
     * @return
     */
    @Override
    public Result<Patient> getPatientByIdCard(String idCard) {
        QueryWrapper<Patient> wrapper = new QueryWrapper<>();
        wrapper.eq("id_card", idCard);
        Patient patient = patientMapper.selectOne(wrapper);
        if (patient == null) {
            return new Result<>(2000, "请输入详细信息,完善患者库", null);
        }
        return new Result<>(2000, "通过 身份证号(idCard) 查看此患者信息", patient);
    }

    @Override
    public Result<Map<String, Object>> patientAllObject(String patientId) {
        QueryWrapper<Patient> wrapper = new QueryWrapper<>();
        wrapper.eq("patient_id", patientId);
        Patient patient = patientMapper.selectOne(wrapper);
        patient.setAge(IDCardUtil.getAge(patient.getIdCard()));

        QueryWrapper<PatientFile> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("patient_id", patientId);
        PatientFile patientFile = patientFileMapper.selectOne(wrapper1);

        QueryWrapper<CareHistory> wrapper2 = new QueryWrapper<>();
        wrapper2.eq("patient_id", patientId);
        List<CareHistory> careHistories = careHistoryMapper.selectList(wrapper2);

        Map<String, Object> map = new HashMap<>();
        map.put("patientObj", patient);
        map.put("patientFileObj", patientFile);
        map.put("careHistoryObjList", careHistories);
        return new Result<>(2000, "成功", map);
    }
}
